import 'package:omni_date_picker/omni_date_picker.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late DateTime _selectedDateTime = DateTime.now();
  late DateTime _startTime = DateTime.now();
  late DateTime _endTime = DateTime.now();
  late Map<dynamic ,dynamic> _filterResult = {};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(" Date Picker Demo"),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Text("官方时间选择器"),
                ElevatedButton(
                    onPressed:(){
                    showDatePicker(
                      context: context,
                      firstDate: DateTime(2025 , 6, 1),
                      lastDate: DateTime.now(),
                    );
                  },
                  child: Text("官方 showDatePicker")
                ),

                Divider(),
                // --- 示例 : 编辑完整日期 ---
                Text("封装时间选择器sdk"),
                const Text("选择结果:", style: TextStyle(fontWeight: FontWeight.bold)),
                Text(_selectedDateTime.toString()),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.editDate,
                      initialDateTime: _selectedDateTime,
                      locale: Locale("en" , "EN"),
                      onConfirm: (dateTime) {
                        // 4. 在回调中用 setState 更新状态
                        setState(() {
                          _selectedDateTime = dateTime;
                        });
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('已选择: $dateTime')),
                        );
                      },
                    );
                  },
                  child: const Text('编辑日期 (年/月/日 时:分)'),
                ),
                const Divider(height: 32),

                // --- 示例 : 按周筛选 ---
                const Text("筛选结果:", style: TextStyle(fontWeight: FontWeight.bold)),
                Text(_filterResult.toString()),
                const SizedBox(height: 8),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.filterDate,
                      filterType: FilterType.week, // 指定筛选类型为周
                      onConfirm: (result) {
                        setState(() {
                          _filterResult = result;
                        });
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('已筛选: $result')),
                        );
                      },
                    );
                  },
                  child: const Text('筛选时间 (按周)'),
                ),
                const Divider(height: 32),
                // --- 示例 : 自定义标题 ---
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.editDate,
                      title: "请选择一个幸运日", // [NEW] 使用自定义标题
                      onConfirm: (dateTime) {},
                    );
                  },
                  child: const Text('编辑日期 (自定义标题)'),
                ),
                const SizedBox(height: 16),
                //  --- 示例：编辑提醒时间
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.editTime,
                      onConfirm: (dateTime) {
                        print("dateTime：$dateTime");
                      },
                    );
                  },
                  child: const Text('提醒日期'),
                ),
                const SizedBox(height: 16),
                // --- 示例 : 自定义周筛选UI ---
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.filterDate,
                      filterType: FilterType.week,
                      title: "选择统计周期",
                      onConfirm: (result) {},
                      weekItemBuilder: (context, weekData, isSelected) {
                        final dateRange = weekData.split('(').first;
                        final isThisWeek = weekData.contains('本周');
                        return Card(
                          color: isSelected ? Colors.blue.shade50 : Colors.white,
                          elevation: isSelected ? 2 : 0,
                          child: Center(
                            child: Row(
                              mainAxisAlignment:MainAxisAlignment.center,
                              children: [
                                Text(
                                  dateRange,
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                                    color: isSelected ? Colors.blue.shade800 : Colors.grey.shade700,
                                  ),
                                ),
                                if(isThisWeek)
                                  const Icon(Icons.star, color: Colors.amber, size: 16)
                              ],
                            ),
                          ),
                        );
                      },
                    );
                  },
                  child: const Text('筛选时间-周 (自定义UI)'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.filterDate,
                      filterType: FilterType.year,
                      initialDateTime: _startTime,
                      showLaterTime: false, // 开始时间通常不能是未来
                      onConfirm: (date) {
                        setState(() {
                          print(date);
                        });
                      },
                    );
                  },
                  child: const Text('筛选时间-年'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.filterDate,
                      filterType: FilterType.month,
                      initialDateTime: _startTime,
                      showLaterTime: false,
                      onConfirm: (date) {
                        setState(() {
                          print(date);
                        });
                      },
                    );
                  },
                  child: const Text('筛选时间-月'),
                ),
                const SizedBox(height: 16),
                // --- 示例 : 设置开始和结束时间 ---
                const Text("开始时间:", style: TextStyle(fontWeight: FontWeight.bold)),
                Text(_startTime.toLocal().toString().split(' ')[0]),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.startTime,
                      initialDateTime: _startTime,
                      showLaterTime: false, // 开始时间通常不能是未来
                      onConfirm: (date) {
                        setState(() {
                          _startTime = date;
                          // 如果结束时间早于新的开始时间，则重置结束时间
                          if (_endTime.isBefore(_startTime)) {
                            _endTime = _startTime;
                          }
                        });
                      },
                    );
                  },
                  child: const Text('选择开始时间'),
                ),
                const SizedBox(height: 16),
                const Text("结束时间:", style: TextStyle(fontWeight: FontWeight.bold)),
                Text(_endTime.toLocal().toString().split(' ')[0]),
                ElevatedButton(
                  onPressed: () {
                    OmniDatePicker.show(
                      context: context,
                      mode: PickerMode.endTime,
                      initialDateTime: _endTime,
                      startTime: _startTime, // 关键：传入开始时间作为限制
                      onConfirm: (date) {
                        setState(() {
                          _endTime = date;
                        });
                      },
                    );
                  },
                  child: const Text('选择结束时间'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
